package com.taptech.doufu.util;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;


public class XmlOperate {

	public static String createXMLFile(String path) {

		String xmlStr = null;

		try {

		Result resultXml = new StreamResult(new FileOutputStream(path));

		StringWriter writerStr = new StringWriter();

		SAXTransformerFactory sff = (SAXTransformerFactory) SAXTransformerFactory.newInstance();

		TransformerHandler th = sff.newTransformerHandler();

		Transformer transformer = th.getTransformer();

		transformer.setOutputProperty(OutputKeys.INDENT, "yes");

		transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

		th.setResult(resultXml);

		th.startDocument();

		AttributesImpl attr = new AttributesImpl();
		
		th.startElement("", "", "items", attr);
		
		th.endElement("", "", "items");
		
		th.endDocument();

		xmlStr = writerStr.getBuffer().toString();

		} catch (TransformerConfigurationException e) {

		e.printStackTrace();

		} catch (SAXException e) {

		e.printStackTrace();

		} catch (Exception e) {

		e.printStackTrace();

		}

		return xmlStr;

		}
	/**
	 * 添加节点
	 */
	public static void addNode(String path, ExceptionInfo info)
	{
		DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();
		try
		{
			DocumentBuilder db=dbf.newDocumentBuilder();
			InputStream is = new FileInputStream(path);
		    Document doc=db.parse(is);
		    Element item=doc.createElement("item");
		    //时间
		    Attr arrTime=doc.createAttribute("time");
		    arrTime.setValue(info.getTime());
		    item.setAttributeNode(arrTime);
		    //时间毫秒数
		    Attr arrTimeInMillis=doc.createAttribute("timeinmillis");
		    arrTimeInMillis.setValue(String.valueOf(info.getTimeInMillis()));
		    item.setAttributeNode(arrTimeInMillis);
		    //内容
		    Attr arrContent=doc.createAttribute("content");
		    arrContent.setValue(info.getContent());
		    item.setAttributeNode(arrContent);
            
            Element eltRoot=doc.getDocumentElement();
            //将student节点添加为根元素的子节点
            eltRoot.appendChild(item);
            Source xmlSource = new DOMSource(doc);
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            Result result = new StreamResult(new File(path));
            transformer.transform(xmlSource, result); // 保存dom至目的文件

		}catch(Exception ex)
		{
			
		}

	}

}
